//
//  ThrowingBallGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/17/13.
//
//

#include "ThrowingBallGameScene.h"
#include "MyUtilities.h"
#include "SceneManager.h"
#include "GameLayerManager.h"
#include "MyThrowingBallButton.h"
CCScene* ThrowingBallGameScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ThrowingBallGameScene *layer = ThrowingBallGameScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->throwingBallGameLayer = layer;
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
    
}

bool ThrowingBallGameScene::init(){
    CountDownGameScene::init();
    ballArray = CCArray::create();
    ballArray->retain();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    int numberOfBalls = 4;
    double ballSize = winSize.width/6;
    double offset = (winSize.width - ballSize*numberOfBalls)/(numberOfBalls+1);
    
    
    for(int i=0;i<numberOfBalls;i++){
        MyThrowingBallButton* iBall = MyThrowingBallButton::create();
        iBall->initialize("circle_button.png", (i+1)*offset + i*ballSize, winSize.height*0.2f, ballSize, ballSize);
        iBall->originalPosition = iBall->getPosition();
        iBall->offsetAround = offset;
        this->addChild(iBall);
        ballArray->addObject(iBall);
        
    }
    double movingBallSize = winSize.width/4;
    movingBall = MyLeftRightMovingButton::create();
    movingBall->initialize("circle_button.png", (winSize.width-movingBallSize)/2, (winSize.height-scoreAndTimeLayer->getBarHeight()-movingBallSize), movingBallSize, movingBallSize);
    movingBall->originalPosition = movingBall->getPosition();
    this->addChild(movingBall);
    
    return true;
}

ThrowingBallGameScene* ThrowingBallGameScene::create(int numberOfSeconds){
    ThrowingBallGameScene* scene = ThrowingBallGameScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}

void ThrowingBallGameScene::scorePoint(){
    numberOfTap++;
    scoreAndTimeLayer->updateScore(numberOfTap);
}

void ThrowingBallGameScene::startGame(){
    CountDownGameScene::startGame();
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->throwingBallGameScene;
    scheduleUpdate();
}

void ThrowingBallGameScene::reset(){
    CountDownGameScene::reset();
    for(int i=0;i<ballArray->count();i++){
        MyThrowingBallButton* iBall = (MyThrowingBallButton*)ballArray->objectAtIndex(i);
        iBall->goBackToOriginalPosition();
    }
    movingBall->goBackToOriginal();

}

void ThrowingBallGameScene::update(float dt){
    for(int i=0;i<ballArray->count();i++){
        MyThrowingBallButton* iBall = (MyThrowingBallButton*)ballArray->objectAtIndex(i);
        iBall->handleUpdate(dt);
    }
    movingBall->handleUpdate(dt);
}

void ThrowingBallGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<ballArray->count();i++){
        MyThrowingBallButton* iBall = (MyThrowingBallButton*)ballArray->objectAtIndex(i);
        iBall->handleMultiTouchesBegan(touches, pEvent);
    }
}

void ThrowingBallGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<ballArray->count();i++){
        MyThrowingBallButton* iBall = (MyThrowingBallButton*)ballArray->objectAtIndex(i);
        iBall->handleMultiTouchesMoved(touches, pEvent);
    }
}

void ThrowingBallGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<ballArray->count();i++){
        MyThrowingBallButton* iBall = (MyThrowingBallButton*)ballArray->objectAtIndex(i);
        iBall->handleMultiTouchesEnded(touches, pEvent);
    }
}

